<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <!-- <el-option label="空闲" :value="0" />
          <el-option label="使用中" :value="1" />
          <el-option label="维修中" :value="2" /> -->
          <el-option 
            v-for="dict in charger_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value" 
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['charger:chargers:add']"
        >新增</el-button>
      </el-col>
      
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['charger:chargers:remove']"
        >删除</el-button>
      </el-col>
      
      
    </el-row>

    <el-table v-loading="loading" :data="chargersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" width="50"/>
      <el-table-column label="名称" align="center" prop="name" width="150"/>
      <el-table-column label="类型" align="center" prop="typeId" width="150"/>
      <el-table-column label="图片" align="center" prop="image" width="120">
        <template #default="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="locationId" width="150" />
      <el-table-column label="连接器类型" align="center" prop="connectorType" width="120"/>
      <el-table-column label="输出功率(kW)" align="center" prop="powerOutput" width="120"/>
      <!-- (0空闲 1使用中 2维修中) -->
      <el-table-column label="状态" align="center" prop="status" >
        <template #default="scope" >
          <el-tag v-if="scope.row.status == 0" type="success">空闲</el-tag>
          <el-tag v-else-if="scope.row.status == 1" type="warning">使用中</el-tag>
          <el-tag v-else-if="scope.row.status == 2" type="danger">维修中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['charger:chargers:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['charger:chargers:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充电桩对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="chargersRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择类型" >
            <el-option
              v-for="dict in chargerTypeOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value" 
            />
          </el-select>  
        </el-form-item>

        <el-form-item label="地址" prop="locationId">
          <el-select v-model="form.locationId" placeholder="请选择地址" >
            <el-option
              v-for="dict in chargerLocationOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value" 
            />
          </el-select>
        </el-form-item>
        
        <el-form-item label="连接器" prop="connectorType" >
          <el-input v-model="form.connectorType" placeholder="请输入连接器类型" />
        </el-form-item>
        <el-form-item label="输出功率" prop="powerOutput">
          <el-input v-model="form.powerOutput" placeholder="请输入输出功率(kW)" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio 
              v-for="(item) in charger_status" 
              :key="item.value" 
              :label="item.value"
              :vakue="item.value" 
               >{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" maxlength="100" show-word-limit/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Chargers">
import { listChargers, getChargers, delChargers, addChargers, updateChargers } from "@/api/charger/chargers";

import { getAllLocations } from "@/api/charger/locations";
import { getAllTypes } from "@/api/charger/types";

const { proxy } = getCurrentInstance();

const chargersList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const chargerLocationOptions = ref([]);
const chargerTypeOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    status: null,
  },
  rules: {
    name: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    locationId: [
      { required: true, message: "地址不能为空", trigger: "blur" }
    ],
    connectorType: [
      { required: true, message: "连接器类型不能为空", trigger: "blur" }
    ],
    typeId: [
      { required: true, message: "充电桩类型不能为空", trigger: "blur" }
    ],
    powerOutput: [
      { required: true, message: "输出功率不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态 (0空闲 1使用中 2维修中)不能为空", trigger: "change" }
    ],
    
  }
});

const { queryParams, form, rules } = toRefs(data);

onMounted(() => { 
  getAllLocationList();
  getAllTypeList();
});

// 获取地址下拉框数据
const getAllLocationList = () => {
  getAllLocations().then(response => {
    chargerLocationOptions.value = response.data;
  });
}

// 获取充电桩类型下拉框数据
const getAllTypeList = () => {
  getAllTypes().then(response => {
    chargerTypeOptions.value = response.data;
  });
}

/** 查询充电桩列表 */
function getList() {
  loading.value = true;
  listChargers(queryParams.value).then(response => {
    chargersList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    image: null,
    locationId: null,
    typeId: null,
    connectorType: null,
    powerOutput: null,
    status: null,
    remark: null,
    createBy: null,
    updateBy: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("chargersRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加充电桩";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getChargers(_id).then(response => {
    form.value = response.data;
    /* ------------------------------------------------ */
    form.value.status = form.value.status.toString();//转换为字符串
    open.value = true;
    title.value = "修改充电桩";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["chargersRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateChargers(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addChargers(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除充电桩编号为"' + _ids + '"的数据项？').then(function() {
    return delChargers(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('charger/chargers/export', {
    ...queryParams.value
  }, `chargers_${new Date().getTime()}.xlsx`)
}

getList();

/* 引用数据字典 */
const { charger_status } = proxy.useDict('charger_status');
</script>
